/**
 * @Description: 配置信息问答器
 * @author Beon
 * @date 2021/1/14
*/
import {OraLoading} from '../decorators/oraLoading';
import * as inquirer from 'inquirer';
import {LzgitCliFile} from '../utils/lzgitCliFile';

export class ConfigInquirer{
    @OraLoading.beProxy
    static status: OraLoading;
    private static configList = ['jira']
    private static _jira: string;
    private static jiraInquirer = {
        type: 'input',
        name: 'inputConfig',
        message: '请输入jira地址',
    }

    static get jira(): string {
        return this._jira;
    }

    /**
     * 输入基础信息
     * @returns {Promise<void>}
     */
    private static async inputMessage(type: string): Promise<string> {
        const getMessage: {inputConfig: string} = await inquirer
            .prompt(this[`${type}Inquirer`]);

        LzgitCliFile.wirteFile({
            [type]: getMessage.inputConfig
        });

        return getMessage.inputConfig;
    }

    /**
     * 检查配置
     * @returns {Promise<boolean>}
     */
    static async checkConfig(): Promise<void> {
        try {
            const json = LzgitCliFile.getFileData(ConfigInquirer.configList);

            for (const item of ConfigInquirer.configList) {
                if (!json || !json[item]){
                    this.status.oraLoadingStop();

                    json[item] = await this.inputMessage(item);

                    this.status.oraLoadingStart();
                }
            }

            ConfigInquirer.configList.forEach((item) => this[`_${item}`] = json[item]);
        } catch (e) {
            console.log(e);
        }
    }
}
